﻿///   <reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js" />


$(document).ready(
function () {
    $.ajax({
        url: 'http://localhost:1234/TwitterChatService.svc/Channels/All',
        async: false,
        dataType: 'json',
        success: function (data) {
            for (i in data) {
                $('#channels').append('<li id="' + data[i].ID + '" value="' + data[i].ChannelName + '">' + data[i].ChannelName + '</li>');
            }

            $('#channelNameHeader').html('Channel: ' + data[0].ChannelName);
            $('#channels li:first').addClass('selected');
        }
    });

    GetMessagesByChannel();

    // $('#chatWindowArticle').scrollTop($('#chatWindowArticle')[0].scrollHeight);

    $('#channels li').click(function () {
        ClearChatWindowContent();
        $('#channels li').removeClass('selected');
        $(this).addClass('selected');
        GetMessagesByChannel();
    });

    $('#resetChannelButton').click(function () {
        ClearChatWindowContent();
    });

    $('#postMessage').click(function () {
        PostNewMessage();
    });

    $("#inputMessage").keyup(function (event) {
        if (event.keyCode == 13) {
            $("#postMessage").click();
        }
    });

    var timer = setInterval('GetNewMessages()', 500);
});

function PostNewMessage() {
    var m = new Object();
    m.Text = $('#inputMessage').val();
    var currentChannel = $('.selected').html();
    m.Channel = currentChannel;
    //var postingDate = new Date();
    m.PostingDate = '[TE SEGA TE]';

    var dataToSend = '{"msg":' + JSON.stringify(m) + '}';

    $.ajax({
        type: "POST",
        url: "http://localhost:1234/TwitterChatService.svc/PostMessage",
        data: dataToSend,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            
        }
    });
    $('#inputMessage').val('');
}

function GetNewMessages() {
    //ClearChatWindowContent();
    var windowScrollDiv = document.getElementById("chatWindowArticle");
    var currentChannel = $('.selected').html();
    var link = "http://localhost:1234/TwitterChatService.svc/GetNewMessages/" + currentChannel;
    $.ajax({
        url: link,
        async: false,
        dataType: 'json',
        success: function (data) {
            for (i in data) {
                $('#chatWindowContent').append('<span style="clear:both;text-align:left;">' + data[i].PostingDate + ' ' + data[i].MessageText + '</span><br />');
                //if ((windowScrollDiv.scrollHeight - windowScrollDiv.scrollTop) == windowScrollDiv.clientHeight) {
                windowScrollDiv.scrollTop = windowScrollDiv.scrollHeight;
                
                
            }
            $('#chatWindowContent span:odd').css('color', 'Red');
        }
    });
}

function GetMessagesByChannel() {
    ClearChatWindowContent();
    $('#channelNameHeader').html('');
    var currentChannel = $('.selected').html();
    $('#channelNameHeader').html('Channel: ' + currentChannel);
    var link = "http://localhost:1234/TwitterChatService.svc/GetAllMessages/" + currentChannel;
    $.ajax({
        url: link,
        async: false,
        dataType: 'json',
        success: function (data) {
            for (i in data) {
                $('#chatWindowContent').append('<span style="clear:both;text-align:left;">' + data[i].PostingDate + ' ' + data[i].MessageText + '</span><br />');
                $('#chatWindowContent').append('<span style="clear:both;text-align:left;">' + data[i].PostingDate + ' ' + data[i].MessageText + '</span><br />');
                $('#chatWindowContent').append('<span style="clear:both;text-align:left;">' + data[i].PostingDate + ' ' + data[i].MessageText + '</span><br />');
            }
            $('#chatWindowContent span:odd').css('color', 'Red');
        }
    });
}

function ClearChatWindowContent() {
    $('#chatWindowContent').empty();
}
